// pages/feedback/feedback.js
import tips from '../../lib/tips';
var util = require('../../utils/util.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    content: null,
    tipsData: {
      title: '',
      isHidden: true
    },
    openId: app.globalData.openId
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  //监听内容输入
  listenerContentInput(e) {
    this.data.content = e.detail.value;
  },
  showToast(title, duartion) {
    const that = this;
    const tipsData = {
      title: title || '',
      duartion: duartion || 2000,
      isHidden: false
    };
    tips.toast(that.data.tipsData);
    that.setData({
      tipsData
    });
    setTimeout(() => {
      tipsData.isHidden = true;
      that.setData({
        tipsData
      });
    }, tipsData.duartion);
  },
  formSubmit: util.throttle(function(e){
    const that = this;
    if (!this.data.content) {
      that.showToast('请输入您要反馈的内容');
      return;
    }
    wx.request({
      url: app.globalData.apiDomain+'/api/feedback/add',
      method:"POST",
      data:{
        content:this.data.content,
        avatarUrl: app.globalData.userInfo.avatarUrl,
        openId: app.globalData.openId,
        nickName: app.globalData.userInfo.nickName
      },
      success:function(res){
        wx.switchTab({
          url: '../userCenter/userCenter',
          complete:function(e){
            wx.showToast({
              title: '反馈成功',
              image: '../../images/correct_toast.png',
              duration: 2000,
            });
          }
        })
      }
    })
  })
})